package com.see.client;

import java.util.HashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import com.see.domain.MessegeParser;
import com.see.domain.Protocol;
import com.see.exception.MessegeFeedException;

public class SendOrderListener implements Listener {

	TradingClientImpl tradingClient;
	private volatile boolean sendOrderSuccsess = false;
	private volatile int idorder = 0;
	private volatile String listnerResult;
	private CountDownLatch latch;
	
	public SendOrderListener(TradingClientImpl tradingClient) {
		this.tradingClient = tradingClient;
		latch = new CountDownLatch(1);
	}
	

	@Override
	public void notificate(HashMap<String, String> paramMap) throws MessegeFeedException {
		String answer = "";
		String cmd = paramMap.get(MessegeParser.MsgFields.TYPEMSG.name());
		if(cmd.equals(Protocol.SENDORDED_FIELD)){
			answer = paramMap.get(MessegeParser.MsgFields.SUCCESS.name());
			
			if (answer.equals(Protocol.TRUE_FIELD)) {
				idorder = Integer.parseInt(paramMap.get(MessegeParser.MsgFields.ORDERID.name()));
				sendOrderSuccsess = true;
			} else {
				listnerResult =  paramMap.get(MessegeParser.MsgFields.ERROR.name());
			}
			latch.countDown();
		}
		
	}

	@Override
	public void waitForComplete() {
		try {
			latch.await(10, TimeUnit.SECONDS);
		} catch (InterruptedException e) {
			//don't matter
		}
		
	}


	public boolean isSendOrderSuccsess() {
		return sendOrderSuccsess;
	}


	public String getListnerResult() {
		return listnerResult;
	}
	
	public int getIdorder() {
		return idorder;
	}

	
	

}